/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb;

import com.google.common.collect.Sets;
import com.hyts.infodb.config.SpringContextConfiguration;
import com.hyts.infodb.metrics.shutdown.ShutdownConfiguration;
import com.hyts.infodb.utils.SpringContextHolder;
import com.hyts.infodb.web.BootApplicationTemplate;
import com.spring4all.swagger.EnableSwagger2Doc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;


/**
 * @author LiBo/Alex
 * @version V1.0 
 */
@Slf4j
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class Laucher extends SpringBootServletInitializer {


    /**
     * 操作启动
     * @param args
     */
    public static void main(String[] args) {
        BootApplicationTemplate.boot(Laucher.class,args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return super.configure(builder);
    }

    /**
     * @project-name:infodb
     * @package-name:com.hyts.infodb.config
     * @author:LiBo/Alex
     * @create-date:2020-01-16 19:10
     * @copyright:libo-alex4java
     * @email:liboware@gmail.com
     * @description:
     */
    @Configuration
    public static class PlatformConfiguration implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {


        @Autowired
        ShutdownConfiguration shutdownConfiguration;


        @Bean(name="localeChangeInterceptor")
        public LocaleChangeInterceptor localeChangeInterceptor(){
            return new LocaleChangeInterceptor();
        }

        @Bean(name="localeResolver")
        public SessionLocaleResolver sessionLocaleResolver(){
            return new SessionLocaleResolver();
        }


        @Override
        public void customize(TomcatServletWebServerFactory factory) {
            factory.addConnectorCustomizers(shutdownConfiguration);
        }


        //@Bean
        public ConfigurableServletWebServerFactory webServerFactory(final ShutdownConfiguration shutdownConfiguration) {
            TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
            tomcatServletWebServerFactory.addConnectorCustomizers(shutdownConfiguration);
            return tomcatServletWebServerFactory;
        }

        /**
         * @project-name:infodb
         * @package-name:com.hyts.infodb.config
         * @author:LiBo/Alex
         * @create-date:2020-01-16 17:14
         * @copyright:libo-alex4java
         * @email:liboware@gmail.com
         * @description:
         */
        @Component
        public static class WebMvcConfiguration extends WebMvcConfigurerAdapter {

            @Autowired
            PlatformInterceptor interceptor;


            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/main").setViewName("main");
                registry.addViewController("/top").setViewName("frame/top");
                registry.addViewController("/left").setViewName("frame/left");
                registry.addViewController("/right").setViewName("frame/right");
                registry.addViewController("/index").setViewName("frame/index");
                registry.addViewController("/error404").setViewName("error");
                registry.addViewController("/search").setViewName("search");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(interceptor).addPathPatterns("/signin");
            }

        }

        /**
         * @project-name:infodb
         * @package-name:com.hyts.infodb.aspect
         * @author:LiBo/Alex
         * @create-date:2020-02-22 10:21
         * @copyright:libo-alex4java
         * @email:liboware@gmail.com
         * @description: 平台总体综合过滤器（配置佩戴相关的信息信息系统）
         */
        @Component
        public static class PlatformInterceptor extends HandlerInterceptorAdapter {

            @Autowired
            Environment environment;

            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                return super.preHandle(request, response, handler);
            }

            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
                modelAndView.addObject("system_profiles",environment.getActiveProfiles()[0]);
            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            }

            @Override
            public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                super.afterConcurrentHandlingStarted(request, response, handler);
            }
        }
    }
}
